package com.wtgroup.demo.面试题.我的面试;

import com.wtgroup.demo.mianshi.线程.d01_wait_notify.D;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * @see D
 * @author nisus
 * @version 0.1
 * @since 2023/6/27 7:03
 */
public class Q_生产者消费者 {

    // 事后感觉应该这么写
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);

        new Thread(() -> {
            int i = 0;
            while (true) {
                try {
                    Thread.sleep(1000);
                    queue.put(++i);
                    System.out.println("生产数据: " + i);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            while (true) {
                Integer poll = null;
                try {
                    poll = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("消费数据: " + poll);
            }
        }).start();

        Thread.currentThread().join();
    }

}
